from typing import List, Iterable

from PySide6.QtGui import QPainter, QPen, Qt, QFont

from common.resources import Resource
from gui.reference import LW, Pos
from gui.widgets_map import open_window
from utils.db import DBSession
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from common.player.reference import Player


class Npc:
    def __init__(self, key, **kwargs):
        self.key = key
        self.name = kwargs['name']

    def draw(self, painter: QPainter, pos: Pos):
        x, y = pos.to_xy()
        painter.save()
        painter.setPen(QPen(Qt.GlobalColor.blue, 3))

        font = QFont()
        font.setFamily('SimHei')
        font.setPointSize(10)
        painter.setFont(font)
        painter.drawText(x + 2, y + 2, LW - 4, LW - 4, Qt.AlignmentFlag.AlignCenter, self.name)
        painter.restore()

    def interaction(self, player: 'Player'):
        pass

    def __str__(self):
        return self.name


class Summoner(Npc):
    pass


class Grocer(Npc):
    def __init__(self, *args, **kwargs):
        self.shop: List[str] = kwargs.pop('shop')
        Npc.__init__(self, *args, **kwargs)

    def interaction(self, player: 'Player'):
        data: Iterable[Resource] = [DBSession.materials[key] for key in self.shop]
        open_window('npc_shop', self.name, player, 'material', data)
